package com.uchengwang.ui.utils;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.ui.cm_ui.ucity.adapter.ChooseItemReasonAdapter;
import com.uchengwang.ui.view.listener.PopupChooseItemListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/11/28.
 */

public class BottomChooseUtils {

    private static BottomChooseUtils utils;
    private BottomChooseUtils(){}

    private PopupWindow mPopupWindow;
    private boolean isAnim = true;

    public static BottomChooseUtils getUtils() {
        if (utils ==null){
            utils = new BottomChooseUtils();
        }
        return utils;
    }

    /**
     * 发布退出保存提示
     * */
    public BottomChooseUtils showView(final Context context, String titleS, List<String> reasons, View shoeView, final PopupChooseItemListener listener){
        View view = LayoutInflater.from(context).inflate(R.layout.view_bottom_choose, null, false);
        TextView cancel = (TextView) view.findViewById(R.id.choose_cancel);//取消
        TextView title = (TextView) view.findViewById(R.id.choose_title);//标题
        ListView listView = (ListView) view.findViewById(R.id.choose_lv);

        if (!TextUtils.isEmpty(titleS)){
            title.setText(titleS);
        }
        if (reasons == null){
            reasons = reason();
        }

        listView.setAdapter(new ChooseItemReasonAdapter(context, reasons));

        final List<String> finalReasons = reasons;
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                listener.onResult(i , finalReasons.get(i));
                dismiss();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setOutsideTouchable(true);
        if (isAnim){
            mPopupWindow.setAnimationStyle(R.style.AnimBottom);
        }
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(00000000));
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                UCommonUtil.getUtil().backgroundAlpha(context, 1f);
                dismiss();
            }
        });
        showBottom(context, shoeView);

        return utils;
    }

    /**
     * 显示在底部
     * */
    private BottomChooseUtils showBottom(Context context, View view) {
        if (UCommonUtil.getUtil().isShowSoftInput(context)) {
            UCommonUtil.getUtil().hideSoftInput(context, view);
        }
        if (mPopupWindow != null && !mPopupWindow.isShowing()) {
            UCommonUtil.getUtil().backgroundAlpha(context, 0.5f);
            mPopupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        }
        return this;
    }

    private List<String> reason(){
        List<String> list = new ArrayList<>();
        list.add("重复发布");
        list.add("恶意广告");
        list.add("侮辱诽谤");
        list.add("淫秽色情");
        list.add("敏感问题");
        list.add("虚假信息");
        list.add("非法互动");
        return list;
    }

    public void dismiss() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
        if (utils != null){
            utils = null;
        }
    }
}
